//
//  ViewController.m
//  手势 dome
//
//  Created by 石虎 on 2017/7/26.
//  Copyright © 2017年 shihu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property(nonatomic) CGFloat lastRotation;//最后一次旋转
@end

/**
 UIGestureRecognize的子类类别有以下几种：
 
 UITapGestureRecognizer            //轻拍识别器
 UIPinchGestureRecognizer         //捏合识别器
 UIRotationGestureRecognizer     //旋转识别器
 UISwipeGestureRecognizer       //扫动识别器
 UIPanGestureRecognizer        //拖动识别器
 UILongPressGestureRecognizer //长按识别器
 
 下面的例子我是用UIPinchGestureRecognizer和UIRotationGestureRecognizer通过手势实现图片旋转和缩小放大。
 */

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加捏合手势识别器，changeImageSize:方法实现图片的放大与缩小
    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(changeImageSize:)];
    [self.view addGestureRecognizer:pinchRecognizer];
    
    //添加旋转手势识别器，rotateImage:方法实现图片的旋转
    UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotateImage:)];
    [self.view addGestureRecognizer:rotateRecognizer];

}

#pragma mark -- 捏合手势识别器
-(void)changeImageSize:(UIPinchGestureRecognizer *)recognizer
{
    CGRect frame = self.imageView.frame;
    
    //监听两手指滑动的距离，改变imageView的frame
    frame.size.width = recognizer.scale*128;
    frame.size.height = recognizer.scale*128;
    self.imageView.frame = frame;
    
    //保证imageView中心不动
    self.imageView.center =CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
}

#pragma mark - 旋转手势识别器 监听
-(void)rotateImage:(UIRotationGestureRecognizer *)rotateRecognizer
{
    //改变imageView的角度，使图片旋转
    if ([rotateRecognizer state]==UIGestureRecognizerStateEnded) {
        self.lastRotation = 0.0;
        return;
    }
    CGAffineTransform currentTransform = self.imageView.transform;
    CGFloat rotation = 0.0 - (self.lastRotation - rotateRecognizer.rotation);
    //拿到当前的位置 和 尺寸
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
    self.imageView.transform = newTransform;
    //显示最后一次的位置
    self.lastRotation = rotateRecognizer.rotation;
}


@end
